//
// BrowseResponder
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crackupdaterweb;

import java.io.*;
import javax.servlet.http.*;
import net.jextra.crack.updater.*;
import com.javapepper.core.*;

/**
 * Either pass-through to {@link BrowseDirPage} or send actual file.
 */
public class BrowseResponder extends BaseResponder
{
    // ============================================================
    // Fields
    // ============================================================

    private BrowseDirPage _browseDirPage;

    // ============================================================
    // Constructors
    // ============================================================

    public BrowseResponder()
        throws IOException
    {
        _browseDirPage = new BrowseDirPage();
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    @Override
    public void processRequest( PepSession pepSession, PepMethod method, HttpServletRequest request,
        HttpServletResponse response )
        throws Exception
    {
        PathMapper pathMap = new PathMapper( request );

        CrackUpdaterEntry entry = getUpdater().getEntry( pathMap.getPath() );

        if ( entry.isFile() )
        {
            streamFile( getUpdater().getFile( entry.getPath() ), request, response );
            return;
        }

        _browseDirPage.init( getModel() );
        _browseDirPage.doGet( pepSession, request, response );
    }

    // ----------
    // private
    // ----------

    private void streamFile( File file, HttpServletRequest request, HttpServletResponse response )
        throws IOException
    {
        response.setContentType( request.getSession().getServletContext().getMimeType( request.getRequestURI() ) );
        FileInputStream in = new FileInputStream( file );

        byte[] buffer = new byte[10000];
        for ( int len = in.read( buffer ); len > 0; len = in.read( buffer ) )
        {
            try
            {
                response.getOutputStream().write( buffer, 0, len );
            }
            catch ( IOException ex )
            {
                // Ignore client aborted exceptions. Note, search Class name so not dependent
                // on any particular web server.
                if ( ex.getClass().getName().contains( "ClientAbort" ) )
                {
                    break;
                }
                else
                {
                    // Rethrow.
                    throw ex;
                }
            }
        }
        in.close();
    }
}
